//
//  SevenViewController.m
//  fp
//
//  Created by xingshuai on 2020/11/10.
//

#import "SevenViewController.h"

#import "SetTableViewCell.h"
#import "ZCViewController.h"
#import "GYViewController.h"

@interface SevenViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *leftTitleArr;
@property (nonatomic, copy) NSArray *sectTionArr;

@end

@implementation SevenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
}

-(NSArray *)leftTitleArr {
    if (!_leftTitleArr) {
        _leftTitleArr = @[@[@"Wi-Fi下自动播放",@"清除缓存"],@[@"隐私权政策",@"当前版本",@"关于我们"]];
    }
    return _leftTitleArr;
}

-(NSArray *)sectTionArr {
    if (!_sectTionArr) {
        _sectTionArr = @[@"播放/缓存",@"其他"];
    }
    return _sectTionArr;
}

-(void)setupUI {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.navView.mas_bottom);
    }];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectTionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.leftTitleArr objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == 1 ? 130 : 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 70)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = [self.sectTionArr objectAtIndex:section];
    label.font = [UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:20];
    label.textColor = [UIColor colorWithHexString:@"#3B3B3B"];
    [headView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).offset(15);
        make.bottom.right.equalTo(headView);
        make.height.mas_offset(50);
    }];
    
    return headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
    footLine.backgroundColor = [UIColor colorWithHexString:@"F6F6F6"];
    
    UIView *fotView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 150)];
    fotView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    
    [fotView addSubview:footLine];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"退 出 登 录" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    [button setTitleColor:[UIColor colorWithHexString:@"#AFAFAF"] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithHexString:@"#F3F3F3"]];
    [fotView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(fotView);
        make.width.mas_equalTo(self.tableView.frame.size.width-30);
        make.height.mas_equalTo(52);
    }];
    
    return section == 0 ? footLine : fotView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SetTableViewCell *cell = [SetTableViewCell cellWithTableView:tableView];
    cell.leftStr = [[self.leftTitleArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.isHidenSwitch = NO;
        } else {
            if ([self folderSizeAtPath:XACCachesPath] > 1024) {
                cell.sizeStr = [NSString stringWithFormat:@"%.2fMb",[self folderSizeAtPath:XACCachesPath]/1024/1024];
            } else {
                cell.sizeStr = [NSString stringWithFormat:@"%.2fk",[self folderSizeAtPath:XACCachesPath]/1024];
            }
            
            cell.isHidenLabel = NO;
        }
    } else {
        if (indexPath.row == 1) {
            cell.banBenStr = [XSCusObject getAppVersion];
            cell.isHidenLabel = NO;
        } else {
            cell.isHidenImg = NO;
        }
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {  // 隐私权政策
            ZCViewController *vc = [[ZCViewController alloc]init];
            vc.navString = @"隐私权政策";
            [self.navigationController pushViewController:vc animated:YES];
        } else if (indexPath.row == 2) {   // 关于我们
            GYViewController *vc = [[GYViewController alloc]init];
            vc.navString = @"关于我们";
            [self.navigationController pushViewController:vc animated:YES];
        }
        else {  // 其他
        }
    } else {
        if (indexPath.row == 1) {
            [self clearCache:XACCachesPath];
            [self.tableView reloadData];
        }
    }
}

//遍历文件夹获得文件夹大小
- (float)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize = 0.0;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            if (![fileName containsString:@"USER_CACHE"]) {
                NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                folderSize += [self fileSizeAtPath:absolutePath];
            }
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getDiskCount];
        return folderSize;
    }
    return 0;
}

// 计算文件大小
- (float)fileSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]){
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size;
    }
    return 0;
}

// 清理缓存文件 同样也是利用NSFileManager API进行文件操作，SDWebImage框架自己实现了清理缓存操作，我们可以直接调用。
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            if (![fileName containsString:@"USER_CACHE"]) {
                NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
    }
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
    //[self showToast];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
